﻿using System.ComponentModel;
using System.Reflection;

namespace EOM.TSHotelManagement.Shared
{
    public class EnumHelper
    {
        public string GetEnumDescription(Enum value)
        {
            FieldInfo field = value.GetType().GetField(value.ToString());
            DescriptionAttribute attribute = field?
                .GetCustomAttributes(typeof(DescriptionAttribute), false)
                .FirstOrDefault() as DescriptionAttribute;

            return attribute?.Description ?? value.ToString();
        }

        public int GetEnumValue(Enum value)
        {
            if (value == null)
                throw new ArgumentNullException(nameof(value));

            return Convert.ToInt32(value);
        }

        public int GetEnumValue(int value)
        {
            if (value == null)
                throw new ArgumentNullException(nameof(value));
            return Convert.ToInt32(value);
        }

        public string GetDescriptionByName<TEnum>(string enumName) where TEnum : Enum
        {
            Type enumType = typeof(TEnum);

            if (!Enum.IsDefined(enumType, enumName))
            {
                return null;
            }

            FieldInfo field = enumType.GetField(enumName);
            DescriptionAttribute attribute = field?
                .GetCustomAttributes(typeof(DescriptionAttribute), false)
                .SingleOrDefault() as DescriptionAttribute;

            string description = attribute?.Description ?? enumName;

            return description;
        }
    }
}
